//
//  DogFoodViewController.m
//  dogGroup
//
//  Created by iOS Two on 14-8-22.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "DogFoodViewController.h"
#import "FoodDetailViewController.h"
#import "UIImageView+MJWebCache.h"

@interface DogFoodViewController ()

@end

@implementation DogFoodViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"DogFoodViewController"];
}
-(void)viewDidDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"DogFoodViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]init];
    self.navigationItem.backBarButtonItem = backItem;
    backItem.title = @"返回";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"喂养" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton)];
    self.navigationItem.rightBarButtonItem = right;
    
    if (version >= 7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.13 green:0.76 blue:0.86 alpha:1];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.text = @"狗粮";
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    } else {
        self.navigationItem.title = @"狗粮";
        self.navigationController.navigationBar.tintColor =[UIColor colorWithRed:0.13 green:0.76 blue:0.86 alpha:1];
    }
    if (![self netWorkCheck]) {
        [self alertViewShow:@"网络未连接"];
    } else {
        [self creatController];
        [self getData];
    }
}

-(void)creatController {
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
    image.image = [UIImage imageNamed:@"dididid_01"];
    [self.view insertSubview:image atIndex:0];
    
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight - 64)];
    if (version >= 7) {
        
    } else {
        _table.frame = CGRectMake(0, 0, 320, kHeight -44);
        
    }
    _table.delegate = self;
    _table.dataSource = self;
    _table.rowHeight = 121.5;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    
    UIImageView *background = [[UIImageView alloc]initWithFrame:self.view.frame];
    background.image = [UIImage imageNamed:@"img_mainbackground"];
    [_table setBackgroundView:background];
    
    _web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight - 64)];
    if (version >= 7) {
        
    } else {
        _web.frame = CGRectMake(0, 0, 320, kHeight - 44);
    }
    _web.backgroundColor = [UIColor clearColor];
    _web.opaque = NO;
}

-(void)getData {
    _foodImg = [[NSMutableArray alloc]init];
    _foodLink = [[NSMutableArray alloc]init];
    _foodName = [[NSMutableArray alloc]init];
    _foodComment = [[NSMutableArray alloc]init];

    [self showIndeterminateProgress];
    
    AVQuery *query1 = [[AVQuery alloc]initWithClassName:@"dogFood"];
    [query1 whereKey:@"dogId" equalTo:@"53eacc0ce4b0540d07bda86b"];
    [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error != nil) {
            [self alertViewShow:@"网络未连接"];
        } else {
            for (int i = 0; i < objects.count; i++) {
                AVObject *object = [objects objectAtIndex:i];
                [_foodName addObject:[object objectForKey:@"foodBand"]];
                [_foodImg addObject:[object objectForKey:@"bandLogo"]];
                [_foodLink addObject:[object objectForKey:@"link"]];
                [_foodComment addObject:[object objectForKey:@"brief"]];
            }
        [_table reloadData];
        }
    }];
    
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"howToFeed"];
    [query getObjectInBackgroundWithId:@"53fae0a9e4b0438e1cb7aa40" block:^(AVObject *object, NSError *error) {
        [_web loadHTMLString:[object objectForKey:@"feedWay"] baseURL:nil];

    }];
    [HUD hide:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _foodComment.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    if (indexPath.row < _foodComment.count) {
        UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 121.5)];
        back.image = [UIImage imageNamed:@"img_foodcellback"];
        [cell.contentView addSubview:back];
        
        UIImageView *foodImg = [[UIImageView alloc]initWithFrame:CGRectMake(9, 4, 102, 109)];
        [foodImg setImageURLStr:[_foodImg objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"img_foodback"]];
        [cell.contentView addSubview:foodImg];
        
        UILabel *foodName = [[UILabel alloc]initWithFrame:CGRectMake(119, 15, 100, 18)];
        foodName.text = [_foodName objectAtIndex:indexPath.row];
        foodName.font = [UIFont systemFontOfSize:18];
        foodName.textColor = [UIColor colorWithRed:0.98 green:0.47 blue:0.14 alpha:1];
        [cell.contentView addSubview:foodName];
        
        UILabel *comment = [[UILabel alloc]initWithFrame:CGRectMake(109, 35, 191, 70)];
        comment.text = [_foodComment objectAtIndex:indexPath.row];
        comment.font = [UIFont systemFontOfSize:13];
        comment.numberOfLines = 4;
        comment.textColor = [UIColor colorWithRed:0.53 green:0.25 blue:0.06 alpha:1];
        [cell.contentView addSubview:comment];
        
        if (indexPath.row <= 2) {
            UIImageView *hot = [[UIImageView alloc]initWithFrame:CGRectMake(0, 14, 39.5, 19)];
            hot.image = [UIImage imageNamed:@"img_hot"];
            [cell.contentView addSubview:hot];
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [MobClick event:@"DogFoodVCFoodDetail"];
    FoodDetailViewController *food = [[FoodDetailViewController alloc]init];
    food.url = [_foodLink objectAtIndex:indexPath.row];
    food.foodTitle = [_foodName objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:food animated:YES];
}
-(void)rightButton {
    CATransition *caSwitch = [CATransition animation];
    caSwitch.delegate = self;
    caSwitch.duration = 0.5;
    caSwitch.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    caSwitch.type = @"cube";
    if ([_table superview] != nil) {
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"推荐" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton)];
        self.navigationItem.rightBarButtonItem = right;
        [self.table removeFromSuperview];
        [self.view insertSubview:_web atIndex:1];
    } else {
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"喂养" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton)];
        self.navigationItem.rightBarButtonItem = right;
        [_web removeFromSuperview];
        [self.view insertSubview:_table atIndex:1];
    }
    [[self.view layer] addAnimation:caSwitch forKey:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
